<template>
  <el-dialog
    :title="editable ? 'Edit' : 'Add'"
    :visible.sync="dialogVisible"
    width="30%"
    @close="handleClose"
  >
    <el-form
      ref="form"
      :model="dataForm"
      :rules="rules"
      label-width="130px"
      class="demo-ruleForm"
    >
      <el-form-item label="Book Name" prop="bookName">
        <el-input
          v-model="dataForm.bookName"
          style="width: 280px"
          maxlength="100"
          placeholder="enter book name please"
        />
      </el-form-item>
      <el-form-item label="Book No" prop="bookNo">
        <el-input
          v-model="dataForm.bookNo"
          style="width: 280px"
          maxlength="100"
          placeholder="enter book no please"
        />
      </el-form-item>
      <el-form-item label="Book Type" prop="typeId">
        <el-select
          v-model="dataForm.typeId"
          style="width: 280px"
          placeholder="please select"
        >
          <el-option
            v-for="item in typeOptions()"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Book Category" prop="categoryId">
        <el-select
          v-model="dataForm.categoryId"
          multiple
          style="width: 280px"
          placeholder="please select"
        >
          <el-option
            v-for="item in categoryOptions()"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Book Quote" prop="quote">
        <el-select
          v-model="dataForm.quote"
          multiple
          style="width: 280px"
          placeholder="please select"
        >
          <el-option
            v-for="item in categoryOptions()"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">Cancel</el-button>
      <el-button type="primary" @click="save">Save</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getInfo, saveBook } from '@/api/books'

export default {
  data() {
    return {
      dialogVisible: false,
      editable: false,
      dataForm: {
        bookName: '',
        bookNo: '',
        typeId: '',
        categoryId: []
      },
      rules: {
        bookName: [
          { required: true, message: 'book name is not null', trigger: 'blur' }
        ],
        bookNo: [
          { required: true, message: 'book no is not null', trigger: 'blur' }
        ],
        typeId: [
          { required: true, message: 'book type is not null', trigger: 'blur' }
        ],
        categoryId: [
          {
            type: 'array',
            required: true,
            message: 'book category is not null',
            trigger: ['blur, change']
          }
        ]
      }
    }
  },

  inject: {
    typeOptions: {
      default: () => []
    },
    categoryOptions: {
      default: () => []
    }
  },

  methods: {
    /**
     * save the form
     */
    async save() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          const {
            id, phone, userName, email
          } = this.dataForm
          await saveBook({ id, phone, userName, email })
          this.$message({
            type: 'success',
            message: 'save success'
          })
          this.handleClose()
        }
      })
    },

    handleClose() {
      this.dialogVisible = false
      this.editable = false
      this.$refs.form.resetFields()
      this.dataForm = this.$options.data().dataForm
    },

    async show(id) {
      this.editable = !!id
      if (id) {
        const info = await getInfo({ id })
        Object.assign(this.dataForm, info)
      }
      console.log(444, this.dataForm)
      this.dialogVisible = true
    }
  }
}
</script>
